package com.hpin.mlxteacher.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.LoginBean;
import com.hpin.mlxteacher.bean.LoginResult;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
import com.hpin.mlxteacher.utils.LogUtil;

/**
 * 登录
 * Created by Administrator on 2015/7/14.
 */
public class LoginActivity extends BaseActivity {

    private TextView tv_forget_psw;
    private ImageView iv_delete_psw;
    private EditText et_username;
    private EditText et_psw;
    private TextView tv_login;
    private Intent mIntent;
    private ImageView iv_delete_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        initWidget();
    }

    public void initWidget() {

        et_psw = (EditText) findViewById(R.id.et_psw);
        et_username = (EditText) findViewById(R.id.et_username);
        iv_delete_psw = (ImageView) findViewById(R.id.iv_delete_psw);
        tv_forget_psw = (TextView) findViewById(R.id.tv_forget_psw);
        tv_login = (TextView) findViewById(R.id.tv_login);

        iv_delete_username = (ImageView) findViewById(R.id.iv_delete_username);

        iv_delete_psw.setOnClickListener(this);
        tv_forget_psw.setOnClickListener(this);
        tv_login.setOnClickListener(this);
        iv_delete_username.setOnClickListener(this);


        et_username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_username.setVisibility(et_username.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                    et_username.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_username.setVisibility(et_username.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                        }
                    });

                } else {
                    iv_delete_username.setVisibility(View.INVISIBLE);

                }
            }
        });


        et_psw.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_psw.setVisibility(et_psw.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                    et_psw.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_psw.setVisibility(et_psw.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });

                } else {
                    iv_delete_psw.setVisibility(View.INVISIBLE);
                }
            }
        });


    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_delete_psw:
                //刪除密碼

                et_psw.setText("");

                break;
            case R.id.iv_delete_username:
                //刪除用户名

                et_username.setText("");

                break;

            case R.id.tv_forget_psw:
                //忘記密碼

                mIntent = new Intent(this, ForgetPswActivity.class);
                startActivity(mIntent);

                break;

            case R.id.tv_login:
                //登錄

                login();

                break;

        }

    }

    /**
     * 登陆
     */
    private void login() {


        if (CommonUtils.isNull(et_username.getText().toString().trim())) {
            showToast("请输入用户名");
            return;
        }

        if (!CommonUtils.isPhone(et_username.getText().toString().trim())) {
            showToast("请输入正确的手机号码");
            return;
        }

        if (CommonUtils.isNull(et_psw.getText().toString().trim())) {
            showToast("请输入密码");
            return;
        }

        final LoginBean loginBean = new LoginBean();
        loginBean.phoneNum = et_username.getText().toString().trim();
        loginBean.pwd = et_psw.getText().toString().trim();

        final String loginJson = JSONObject.toJSONString(loginBean);

        LogUtil.e("TAG", "登陆请求的json===>>>" + loginJson);

        HttpUtil.getInstance().setPostRequest("登录中...", this, Constant.WEB_ROOT + Constant.LOGIN, loginJson, new HttpUtil.SuccessListener() {

            @Override
            public void setOnSuccessListener(String str) {

                if (CommonUtils.isNull(str)) {
                    showToast(Constant.NODATA);
                    return;
                }

                try {

                    LoginResult loginResult = JSONObject.parseObject(str, LoginResult.class);

                    if (loginResult.code == 0) {//成功

                        Intent mIntent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(mIntent);

                    } else {
                        //弹出错误提示
                        showToast(Constant.showErrorMsg(loginResult.code));

                    }

                } catch (Exception e) {

                    LogUtil.e("TAG", "json 解析错误==>>>" + str);
                }


            }

        }, null);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        System.exit(0);
    }
}
